package javolution.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Set;
import javolution.context.ObjectFactory;
import javolution.lang.Reusable;
import javolution.util.FastCollection;
import javolution.util.FastMap;

/* loaded from: classes2.dex */
public class FastSet<E> extends FastCollection<E> implements Set<E>, Reusable {
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastSet.1
        @Override // javolution.context.ObjectFactory
        public void cleanup(Object obj) {
            ((FastSet) obj).reset();
        }

        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new FastSet();
        }
    };
    private static final long serialVersionUID = 1;
    private transient FastMap _map;

    public FastSet() {
        this(new FastMap());
    }

    public FastSet(int i) {
        this(new FastMap(i));
    }

    public FastSet(String str) {
        this(new FastMap(str));
    }

    public FastSet(Set<? extends E> set) {
        this(new FastMap(set.size()));
        addAll(set);
    }

    private FastSet(FastMap fastMap) {
        this._map = fastMap;
    }

    public static <E> FastSet<E> newInstance() {
        return (FastSet) FACTORY.object();
    }

    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        int readInt = objectInputStream.readInt();
        this._map = new FastMap(readInt);
        setValueComparator((FastComparator) objectInputStream.readObject());
        while (true) {
            int i = readInt - 1;
            if (readInt == 0) {
                return;
            }
            Object readObject = objectInputStream.readObject();
            this._map.put(readObject, readObject);
            readInt = i;
        }
    }

    public static void recycle(FastSet fastSet) {
        FACTORY.recycle(fastSet);
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.writeObject(getValueComparator());
        objectOutputStream.writeInt(size());
        FastMap.Entry head = this._map.head();
        FastMap.Entry tail = this._map.tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return;
            }
            objectOutputStream.writeObject(head.getKey());
        }
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean add(E e) {
        return this._map.put(e, e) == null;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final void clear() {
        this._map.clear();
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean contains(Object obj) {
        return this._map.containsKey(obj);
    }

    @Override // javolution.util.FastCollection
    public final void delete(FastCollection.Record record) {
        this._map.remove(((FastMap.Entry) record).getKey());
    }

    @Override // javolution.util.FastCollection
    public FastComparator<? super E> getValueComparator() {
        return this._map.getKeyComparator();
    }

    @Override // javolution.util.FastCollection
    public final FastCollection.Record head() {
        return this._map.head();
    }

    @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
    public Iterator<E> iterator() {
        return this._map.keySet().iterator();
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean remove(Object obj) {
        return this._map.remove(obj) != null;
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._map.reset();
    }

    public FastSet<E> setValueComparator(FastComparator<? super E> fastComparator) {
        this._map.setKeyComparator(fastComparator);
        return this;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final int size() {
        return this._map.size();
    }

    @Override // javolution.util.FastCollection
    public final FastCollection.Record tail() {
        return this._map.tail();
    }

    @Override // javolution.util.FastCollection
    public Set<E> unmodifiable() {
        return (Set) super.unmodifiable();
    }

    @Override // javolution.util.FastCollection
    public final E valueOf(FastCollection.Record record) {
        return (E) ((FastMap.Entry) record).getKey();
    }
}
